package com.java;

import com.java.model.ArticleInfo;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class Articles {
    @Bean  //将当前方法返回的对象存储到IoC容器
    public ArticleInfo getArt(){
        //伪代码
        ArticleInfo articleInfo=new ArticleInfo();
        articleInfo.setAid(1);
        articleInfo.setTitle("日期");
        articleInfo.setContent("周一！！！");
        articleInfo.setCreatetime(LocalDateTime.now());
        return articleInfo;
    }

    @Bean("bean1")
    public ArticleInfo getArt1(){
        ArticleInfo articleInfo=new ArticleInfo();
        articleInfo.setAid(1);
        articleInfo.setTitle("日期");
        articleInfo.setContent("周一！！！");
        articleInfo.setCreatetime(LocalDateTime.now());
        return articleInfo;
    }

    @Bean(name="bean2")
    public ArticleInfo getArt2(){
        ArticleInfo articleInfo=new ArticleInfo();
        articleInfo.setAid(1);
        articleInfo.setTitle("日期");
        articleInfo.setContent("周一！！！");
        articleInfo.setCreatetime(LocalDateTime.now());
        return articleInfo;
    }

    @Bean(value="bean3")
    public ArticleInfo getArt3(){
        ArticleInfo articleInfo=new ArticleInfo();
        articleInfo.setAid(1);
        articleInfo.setTitle("日期");
        articleInfo.setContent("周一！！！");
        articleInfo.setCreatetime(LocalDateTime.now());
        return articleInfo;
    }

    @Bean(value={"bean4","bean5"})
    public ArticleInfo getArt4(){
        ArticleInfo articleInfo=new ArticleInfo();
        articleInfo.setAid(1);
        articleInfo.setTitle("日期");
        articleInfo.setContent("周一！！！");
        articleInfo.setCreatetime(LocalDateTime.now());
        return articleInfo;
    }

    //注：当有多个Bean使用相同名称时，程序不会报错，但是第一个Bean之后的对象不会被存放到容器中，
    //也就是只有在第一次时会与Bean名称关联起来，后续相同名称会自动忽略

    public void sayHi(){
        System.out.println("hi articles");
    }
}
